<template>
  <div class="swiper-container swiper1">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="(item) in bannerLists">
        <img :src="item.banner_pic" alt>
      </div>
    </div>
    <div class="swiper-pagination"></div>
  </div>
</template>

<script>
import getData from "../api/getInfo";
export default {
  data() {
    return {
      bannerLists: []
    };
  },
  mounted() {
    this.getBanner();
  },
  methods: {
     initSwiper() {
      let mySwiper = new Swiper(".swiper1", {
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        // autoplay: true,
        loop: true,
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
          // bulletClass: "my-bullet"
          bulletActiveClass: 'my-bullet-active',
        },
        observer: true
      });
    },
    //获取banner图
    getBanner() {
      getData
        .getBanner()
        .then(res => {
          let tempArr = [];
          res.data.forEach((val, index) => {
            if (val.type == 1) {
              val.banner_pic = `${getData.url}${val.banner_pic}`;
              tempArr.push(val);
            }
          });
          this.bannerLists = tempArr;
          this.$nextTick(() => {
            // 下一个UI帧再初始化swiper
            this.initSwiper();
          });
        })
        .catch(e => {
          console.log(e);
        });
    }
  }
};
</script>
<style scoped lang="scss">
.swiper-container {
  height: 505px;
  width: 100%;
}
</style>
